package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ShipAddressActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ShipAddressActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.NewAddressEvent;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;
import com.wushi.lenovo.asia5b.app_model.result.AddressResModel;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.CancelSureDialog;
import com.wushi.lenovo.asia5b.app_ui.profile.adapter.ShipAddressAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收货地址管理
 */
public class ShipAddressActivity extends BaseMvpActivity<
        ShipAddressActivityView, ShipAddressActivityPresenter> implements ShipAddressActivityView {

    @BindView(R.id.recyclerview_shipaddress)
    RecyclerView mRecyclerView;

    private ShipAddressAdapter mAdapter;
    private List<AddressModel> mDatas;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shipaddress;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        mDatas = new ArrayList<>();
        mAdapter = new ShipAddressAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        getAddresses();
    }

    private void getAddresses() {
        getPresenter().getAddresses();
    }

    @Override
    protected void initEvent() {
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.iv_ship_edit: {
                    AddressModel model = mAdapter.getItem(position);
                    Intent intent = new Intent(ShipAddressActivity.this, NewAddressActivity.class);
                    intent.putExtra(CommonConstant.ADDRESS_MODEL, model);
                    startActivity(intent);
                }
                break;
                case R.id.iv_ship_delete: {
                    new CancelSureDialog(ShipAddressActivity.this,
                            R.string.yes,
                            R.string.no,
                            R.string.shipaddress_deletenotice,
                            (s) -> {
                                getPresenter().deleteAddress(mAdapter.getItem(position).getId());
                            }).show();
                }
                break;
            }

        });
    }

    @Override
    @OnClick({
            R.id.bt_ship_newaddres
    })
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_ship_newaddres:
                startActivity(new Intent(this, NewAddressActivity.class));
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeAddresses(NewAddressEvent event) {
        if (event != null) {
            getAddresses();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void getAddressesResult(ResultModel<AddressResModel> addressResModelResultModel) {
        mDatas.clear();
        AddressModel shipAddress = null;//增加默认地址
        AddressModel billAddress = null;//增加默认地址

        if (addressResModelResultModel.isSuccess() && addressResModelResultModel.getData() != null) {
            AddressResModel addressResModel = addressResModelResultModel.getData();
            int bill_address_id = addressResModel.getBill_address_id();
            int ship_address_id = addressResModel.getShip_address_id();
            List<AddressModel> addresses = addressResModel.getAddresses();

            if (addresses != null) {
                for (AddressModel model : addresses) {
                    if (model.getId() == ship_address_id) {
                        shipAddress = model;
                    }
                    if (model.getId() == bill_address_id) {
                        billAddress = model;
                    }
                    if (model.getId() != ship_address_id && model.getId() != bill_address_id) {
                        mDatas.add(model);
                    }
                }
            }


        }

        if (billAddress == null) {
            billAddress = new AddressModel();
        }
        billAddress.setBillAddressDefault(true);
        if (shipAddress == null) {
            shipAddress = new AddressModel();
        }
        shipAddress.setShipAddressDefault(true);
        mDatas.add(0, billAddress);
        mDatas.add(0, shipAddress);
        if (mDatas.size() == 2) {
            mDatas.add(new AddressModel());//增加空布局地址
        }
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void deleteAddressResult(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            ToastUtil.showToast(resultModel.getMsg());
            getAddresses();
        }
    }
}
